package com.plm.maque.domain.vo;

import com.plm.maque.common.page.Page;
import com.plm.maque.common.exception.CodeEnum;
import com.plm.maque.common.exception.IBaseError;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * @author Limi
 * @create 2020-06-27 11:50
 */
@ApiModel("响应数据")
@Getter
@Setter
@Accessors(chain = true)
public class ResultBody extends Page {
    @ApiModelProperty("响应编码")
    private String code;
    @ApiModelProperty("是否成功")
    private boolean success;
    @ApiModelProperty("提示信息")
    private String msg;
    @ApiModelProperty("返回的数据")
    private Object result;

    public ResultBody(){

    }

    public ResultBody(IBaseError iBaseError){
        this.code = iBaseError.getCode();
        this.msg = iBaseError.getMsg();
    }

    public static ResultBody success() {
        return success(null);
    }

    public static ResultBody success(Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(CodeEnum.SUCCESS.getCode());
        rb.setMsg(CodeEnum.SUCCESS.getMsg());
        rb.setResult(data);
        rb.setSuccess(true);
        return rb;
    }

    public static ResultBody error(IBaseError iBaseError) {
        ResultBody rb = new ResultBody();
        rb.setCode(iBaseError.getCode());
        rb.setMsg(iBaseError.getMsg());
        rb.setSuccess(false);
        rb.setResult(null);
        return rb;
    }

    public static ResultBody error(String code, String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMsg(message);
        rb.setSuccess(false);
        rb.setResult(null);
        return rb;
    }

    public static ResultBody error(String message) {
        ResultBody rb = new ResultBody();
        rb.setCode("-1");
        rb.setSuccess(false);
        rb.setMsg(message);
        rb.setResult(null);
        return rb;
    }

}
